<?php


namespace App\backend\services;


use App\backend\models\AdminRoleMenuModel;
use App\backend\models\RoleModel;
use App\Exceptions\InvalidRequestException;

class RoleServices
{

    /**
     * 获取 - 角色列表
     *
     * @param $data //请求参数数组
     * @return array
     */
    public static function getList($data)
    {
        $page = !isset($data['page']) ? 1 : $data['page'];unset($data['page']);
        $limit = !isset($data['limit']) ? 10 : $data['limit'];unset($data['limit']);

        $list = RoleModel::listed($page, $limit, $data);
        if (empty($list)) {
            throw new InvalidRequestException(10009);
        }

        return $list;
    }

    /**
     * 获取 - 角色详情
     *
     * @param $data
     * @return array
     */
    public static function getDetail($data)
    {
        if (empty($data) && $data == false) {
            return [];
        }

        $data['id'] = !empty($data['role_id']) ? $data['role_id'] : 0;unset($data['role_id']);

        $list = RoleModel::info($data);
        if (empty($list)) {
            throw new InvalidRequestException(10009);
        }
        return $list;
    }

    /**
     * 添加/编辑 - 角色
     *
     * @param $data //请求参数数组
     * @return array
     */
    public static function modify($data)
    {
        if (empty($data) && $data == false) {
            throw new InvalidRequestException(10009);
        }

        $data['id'] = !empty($data['role_id']) ? $data['role_id'] : 0;unset($data['role_id']);

        $where_id_data[] = ['name', '=', $data['name']];
        $id_list = RoleModel::info($where_id_data, ['id']);
        if (empty($data['id']) && !empty($id_list)) {
            throw new InvalidRequestException(20012);
        }

        if ($data['id']) {
            $update_data = [
                'id' => $data['id'],
                'name' => $data['name'],
                'slug' => $data['slug'],
                'updated_at' => time()
            ];
            $res_id = RoleModel::updatedData($data['id'], $update_data);
            if (!empty($res_id)) {
                $res_id = &$data['id'];
                $where_data[] = ['id', '=', $res_id];
            }
        } else {
            $data['created_at'] = time();
            $res_id = RoleModel::insertGetIdData($data);
            if (!empty($res_id)) {
                $where_data[] = ['id', '=', $res_id];
            }
        }

        if (!empty($data['ids'])) {
            $where_role_data = ['role_id'=>$res_id];
            $role_data = AdminRoleMenuModel::getFirst($where_role_data);
            $state = 1;
            if (!empty($role_data)) {
                $state = AdminRoleMenuModel::updatedData($where_role_data, ['status' => 1 , 'updated_at' => time()]);
            }

            if ($state) {
                $ids = explode("," , $data['ids']);
                foreach ($ids as $value) {
                    $insert_data = [
                        'role_id' => $res_id,
                        'menu_id' => $value,
                        'created_at' => time()
                    ];
                    AdminRoleMenuModel::insertData($insert_data);
                }
            }
        }

        $list = RoleModel::info($where_data, ['id', 'name', 'slug']);
        if (empty($list)) {
            throw new InvalidRequestException(10009);
        }
        return $list;
    }

    /**
     * 删除 - 操作员
     *
     * @param $data //请求参数数组
     * @return array
     */
    public static function delete($data)
    {
        if (empty($data) && $data == false) {
            return [];
        }

        $data['id'] = !empty($data['role_id']) ? $data['role_id'] : 0;unset($data['role_id']);
        $update_data['status'] = 1;
        $update_data['deleted_at'] = time();

        $list = RoleModel::deleteds($data, $update_data);
        if (empty($list)) {
            throw new InvalidRequestException(10009);
        }
        return [];
    }

    /**
     * 获取 - 用户个人权限
     *
     * @param $id // 用户ID
     * @return array
     */
    public static function getRoleUser($id)
    {
        if (empty($id)) {
            return [];
        }

        $list = RoleModel::getRoleUser($id);
        if (empty($list)) {
            throw new InvalidRequestException(10009);
        }

        return $list;
    }

}
